package cn.com.tom.servlet;

import cn.com.tom.bean.Skill;
import cn.com.tom.service.SkillService;
import cn.com.tom.service.impl.SkillServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/skill/*")
public class SkillServlet extends BaseServlet {
    SkillService servie = new SkillServiceImpl();

    public void admin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Skill> list = servie.list();
        request.setAttribute("skills", list);
        forward(request, response, "admin/skill.jsp");
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Skill bean = new Skill();
        BeanUtils.populate(bean, request.getParameterMap());
        if (servie.save(bean)) {
            redirect(request, response, "/skill/admin");
        } else {
            forwardError(request, response, "保存失败");
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] idS = request.getParameterValues("id");
        List<Integer> list = new ArrayList();
        for (int i = 0; i < idS.length; i++) {
            list.add(Integer.valueOf(idS[i]));
        }
        if (servie.remove(list)) {
            redirect(request, response, "/skill/admin");
        } else {
            forwardError(request, response, "删除失败");
        }
    }
}
